package com.xcjh.module_ws

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import android.widget.ScrollView
import androidx.activity.OnBackPressedCallback
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.gson.Gson
import com.xcjh.base_lib.Constants
import com.xcjh.base_lib.base.BaseViewModel
import com.xcjh.base_lib.base.activity.BaseVmDbActivity
import com.xcjh.module_ws.databinding.WsActivityMainBinding
import com.xcjh.module_ws.websocket.MyWsManager
import com.xcjh.module_ws.websocket.Ws2MainActivity
import com.xcjh.module_ws.websocket.bean.ChatMsgBean
import com.xcjh.module_ws.websocket.bean.SendChatMsgBean
import com.xcjh.module_ws.websocket.bean.SendCommonWsBean
import com.xcjh.module_ws.websocket.listener.C2CListener
import com.xcjh.module_ws.websocket.listener.WsListener

@Route(path = Constants.Router.Ws.A_Ws_MAIN)
class WsMainActivity : BaseVmDbActivity<BaseViewModel, WsActivityMainBinding>(), OnClickListener {
    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.btnLogin.setOnClickListener(this)
        mDatabind.btnSend.setOnClickListener(this)
        mDatabind.btnGetHistory.setOnClickListener(this)
        mDatabind.btnConnect.setOnClickListener(this)
        mDatabind.btnDisConnect.setOnClickListener(this)
        MyWsManager.getInstance(this)?.setWsListener(javaClass.name,object : WsListener {
            override fun onReceive(msg: String) {
                mDatabind.tvMsg.text = mDatabind.tvMsg.text.toString() + "\n\n" + msg
                mDatabind.tvMsg.postDelayed({
                    mDatabind.scrollView.fullScroll(ScrollView.FOCUS_DOWN)
                }, 200)

            }
        })
        MyWsManager.getInstance(this)?.setC2CListener(object : C2CListener {
            override fun onC2CReceive(chat: ChatMsgBean) {
                Log.e("===============", "onC2CReceive: ===" + chat.content)
              //  myToast(chat.content)
            }
        })
        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                if (System.currentTimeMillis() - exitTime > 2000) {
                    exitTime = System.currentTimeMillis()
                } else {
                    //System.exit(0)
                    finish()
                }
            }
        })
    }

    private var exitTime: Long = 0
    override fun showLoading(message: String) {

    }

    override fun dismissLoading() {

    }

    override fun createObserver() {

    }

    override fun onClick(v: View?) {
        when (v) {
            //用户端
            /*mDatabind.btnLogin -> {
                MyWsManager.getInstance(this)?.sendMessage(
                    Gson().toJson(
                        SendCommonWsBean(5,
                            "1692360254443462656",
                            "IP51QGK1H5ZKf5Lox2VTQTeLF7g7xvqKiYpF7aQCF5XBi9JCuV3WCyTJ5ik0BTlQ")
                    )
                )
            }
            mDatabind.btnSend -> {
                MyWsManager.getInstance(this)?.sendMessage(
                    Gson().toJson(
                        SendChatMsgBean(2, 0, 11,
                            "1692360254443462656",
                            "1689193696521482240",
                            to = "1689193696521482240",
                            content = mDatabind.edMsg.text.toString(),
                            createTime = System.currentTimeMillis()
                        )
                    )
                )
            }*/
            //主播
            mDatabind.btnLogin -> {
                MyWsManager.getInstance(this)?.sendMessage(
                    Gson().toJson(
                        SendCommonWsBean(
                            5,
                            "1689193696521482240",
                            "SDxVhkDrL5fEY5EM76aJ390GG6dDBhRoa5X0mzZhnz4iEnf78NY5J4cmBcbhkOlN",
                            loginType = "2",
                        )
                    )
                )
            }
            mDatabind.btnSend -> {
                MyWsManager.getInstance(this)?.sendMessage(
                    Gson().toJson(
                        /*SendChatMsgBean(2, 0, 11,
                            "1689193696521482240",
                            "1689193696521482240",
                            to = "1692360254443462656",
                            content = mDatabind.edMsg.text.toString(),
                            createTime = System.currentTimeMillis()
                        )*/
                                SendChatMsgBean(2, 0, 11,
                                    "1689193696521482240",
                                    "1689193696521482240",
                                 //   to = "1692360254443462656",
                                    to = "1712746198255689728",
                                    content=mDatabind.edMsg.text.toString(),
                                    identityType="1",
                                    createTime =System.currentTimeMillis(),
                                    toNickName="你是不是傻？",
                                    fromAvatar= "",
                                    fromNickName="主播之国士无双",
                                    level="11",
                    )
                    )
                )
            }
            mDatabind.btnGetHistory -> {
                startActivity(Intent(this,Ws2MainActivity::class.java))
              /*  MyWsManager.getInstance(this)?.sendMessage(
                    Gson().toJson(SendCommonWsBean(19,
                        fromUserId = "1689193696521482240",
                        userId = "1692360254443462656",
                        loginType = null,
                        type = "0"))
                )*/
            }
            mDatabind.btnConnect -> {
                MyWsManager.getInstance(this)?.initService()
            }
            mDatabind.btnDisConnect -> {
                MyWsManager.getInstance(this)?.stopService()
            }
        }
    }


    override fun onPause() {
        Log.e("===", "onPause: ===" )
        super.onPause()
    }
    override fun onStop() {
        Log.e("===", "onStop: ===" )
        super.onStop()
    }
    override fun onDestroy() {
        Log.e("====", "onDestroy: ====")
        MyWsManager.getInstance(this)?.stopService()
        super.onDestroy()
    }

}